<?php		
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'theme-options/home-options.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'theme-options/default-options.php' );	
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'wpsocialite/wpsocialite.php' );	
		
	
	add_action('wp_head', 'cv_home_js');
	function cv_home_js() { 		
		wp_enqueue_script('jquery');?>		
		<script type="text/javascript" language="javascript">
			jQuery(document).ready(function($){
				$('.hentry .entry-img, #portfolio-list .entry-img').hover(function(){
					 $(this).find('strong').stop().fadeTo('normal', 0.3);
				}, function () {
					$(this).find('strong').stop().fadeTo('normal', 0);
				});					

				$("#menu-primary-responsive .current-menu-item ").attr('selected', 'selected');			
				
			});
		
		</script>		
	
		<?php 
	} 
	 
		
	function get_tag_id_by_name($tag_name) {
		global $wpdb;
		$tag_ID = $wpdb->get_var("SELECT * FROM ".$wpdb->terms." WHERE  `name` =  '".$tag_name."'");

		return $tag_ID;
	}
	
	// Variable & intelligent excerpt length.
	function print_excerpt($length = 300) { // Max excerpt length. Length is set in characters
		global $post;
		if(!isset($length)) $length = 300;
		$text = $post->post_excerpt;
		if ( '' == $text ) {
			$text = get_the_content('');
			$text = apply_filters('the_content', $text);
			$text = str_replace(']]>', ']]>', $text);
		}
		$text = strip_shortcodes($text); // optional, recommended
		$text = strip_tags($text); // use ' $text = strip_tags($text,'<p><a>'); ' if you want to keep some tags
		
		$s = '';
		if(strlen($text) > $length)
			$s = '...';
			
		$text = substr($text,0,(int)$length) . $s;
		$excerpt = reverse_strrchr($text, ' ', 1) . $s;
		if( $excerpt ) {			
			echo apply_filters('the_excerpt',$excerpt);
		} else {
			echo apply_filters('the_excerpt',$text);
		}
	}

	// Returns the portion of haystack which goes until the last occurrence of needle
	function reverse_strrchr($haystack, $needle, $trail) {
		return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
	}	
	
	// Tabber: Get Most Popular Posts
	function cv_radom_related_posts( $posts = 5, $cat = '' ) {
		$popular = new WP_Query('orderby=rand&posts_per_page='.$posts.'&category='.$cat);
		$i = 0;
		while ($popular->have_posts()) : $popular->the_post();$i++;
	?>
		<li>
			<div class="feature-img">				
				<?php if ( current_theme_supports( 'get-the-image' ) ) get_the_image( array( 'meta_key' => array( 'Thumbnail', 'thumbnail'), 'default_size' => 'popular-image-thumb', 'default_image' => get_bloginfo('template_directory').'/images/'.'popular-image-thumb.png' ) );?>
			</div>
			<div class="info">
				<h4><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>	
				<div class="comments-info"><a href="<?php echo get_comments_link();?>" ><?php echo get_comments_number();?></a><?php _e(' Comments', hybrid_get_textdomain());?></div>
			</div> <!--end .info-->
			<div class="clear"></div>
		</li>
	<?php endwhile; 
	//wp_reset_query();
	}
	
	// Tabber: Get Most Popular Posts
	function cv_popular_posts( $order_by = 'comment_count', $posts = 5 ) {
		$popular = new WP_Query('orderby='. $order_by . '&posts_per_page='.$posts);
		while ($popular->have_posts()) : $popular->the_post();
	?>
		<li>
			<div class="feature-img">				
				<?php if ( current_theme_supports( 'get-the-image' ) ) get_the_image( array( 'meta_key' => array( 'Thumbnail', 'thumbnail'), 'default_size' => 'popular-image-thumb', 'default_image' => get_bloginfo('template_directory').'/images/'.'popular-image-thumb.png' ) );?>
			</div>
			<div class="info">
				<h4><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>	
				<div class="comments-info"><a href="<?php echo get_comments_link();?>" ><?php echo get_comments_number();?></a><?php _e(' Comments', hybrid_get_textdomain());?></div>	
			</div> <!--end .info-->
			<div class="clear"></div>
		</li>
	<?php endwhile; 
	//wp_reset_query();
	}

	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-flickr.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-popular.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-contact.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-twitter.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-random-related-posts.php' );
	require_once( trailingslashit( THEME_FUNCTIONS_DIR ) . 'widgets/widget-max-social/wp-max-social-widget.php' );

?>